package cn.zjweu.servlet;

import cn.zjweu.domain.DishCategory;
import cn.zjweu.domain.MessageInfo;
import cn.zjweu.domain.ResultInfo;
import cn.zjweu.service.CategoryService;
import cn.zjweu.service.impl.CategoryServiceImpl;
import cn.zjweu.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/category/*")
public class CategoryServlet extends BaseServlet {

    private CategoryService categoryService = new CategoryServiceImpl();

    public void addCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        try {
            String categoryName = request.getParameter("categoryName");
            if (categoryService.addCategory(categoryName)) {
                info.setFlag(MessageInfo.SUCCESS);
                info.setData(categoryName);
                info.setInfoMsg("添加成功");
            } else {
                info.setFlag(MessageInfo.FAIL);
                info.setData(categoryName);
                info.setInfoMsg("添加失败");
            }
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("添加失败");
        } finally {
            writeValue(info, response);
        }
    }

    public void delCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int categoryId = 0;
        try {
            categoryId = Integer.parseInt(request.getParameter("categoryId"));
            if (categoryService.delCategoryById(categoryId)) {
                info.setFlag(MessageInfo.SUCCESS);
                info.setData(categoryId);
                info.setInfoMsg("删除成功");
            } else {
                info.setFlag(MessageInfo.FAIL);
                info.setData(categoryId);
                info.setInfoMsg("删除失败！请先删除分类下的菜品。");
            }
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setData(categoryId);
            info.setInfoMsg("删除失败！请先删除分类下的菜品。");
        } finally {
            writeValue(info, response);
        }
    }

    public void editCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int categoryId = 0;
        try {
            categoryId = Integer.parseInt(request.getParameter("categoryId"));
            String categoryName = request.getParameter("categoryName");
            categoryService.editCategoryById(categoryId, categoryName);
            info.setFlag(MessageInfo.SUCCESS);
            info.setData(categoryId + ": " + categoryName);
            info.setInfoMsg("修改成功");
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("修改失败");
        } finally {
            writeValue(info, response);
        }

    }

    public void findCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        List<DishCategory> categoryList;
        try {
            categoryList = categoryService.findCategory();
            info.setFlag(MessageInfo.SUCCESS);
            info.setData(categoryList);
            info.setInfoMsg("查找分类成功");
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("查找分类失败！");
        } finally {
            writeValue(info, response);
        }
    }

    public void findCategoryById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();

        int categoryId = 0;
        try {
            categoryId = Integer.parseInt(request.getParameter("categoryId"));
            DishCategory dishCategory = categoryService.findCategoryById(categoryId);
            info.setFlag(MessageInfo.SUCCESS);
            info.setData(dishCategory);
            info.setInfoMsg("查找分类成功");
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setData(categoryId);
            info.setInfoMsg("查找分类失败！");
        } finally {
            writeValue(info, response);
        }
    }
}
